<?php

namespace app\system\controller;

use app\common\model\logic\CarDriverModel;
use app\common\model\UserModel;
use think\Cookie;
use think\exception\TemplateNotFoundException;
use think\Request;
use think\Session;

class BaseController extends \think\Controller
{
    private $deptIds;
    private $uid;

    /**
     * 初始化
     * BaseController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        if (is_mobile()==3) {
            $this->view = $this->view->config('view_path', APP_PATH . 'system/view_mobile/');
        } else {
            $this->view = $this->view->config('view_path', APP_PATH . 'system/view_pc/');
        }
        $des = is_login($this->getUtype());
        if ($des === false || $this->getUtype() != $des['utype']) {
            Cookie::delete($this->getUtype().'Des'); //清空cookie
            $this->error('请登录后再操作！', url('Public/Login'));
        } else {
            $this->uid = $des['uid'];
            $this->deptIds = $des['dept_ids'];
            $this->groupId = $this->getGroupId();
            $this->assign('group_id',$this->getGroupId());
            $this->assign('dept_ids', $this->deptIds);
            $this->assign('uid', $this->uid);
        }
    }

    public function fetch($template = '', $vars = [], $replace = [], $config = [])
    {
        if ($this->request->isMobile()) {
            try {
                return parent::fetch($template, $vars, $replace, $config);
            } catch (TemplateNotFoundException $e) {
                $template = str_replace('view_mobile', 'view_pc', $e->getTemplate()); //替换捕获到的模板文件
                if (is_file($template)) {
                    return parent::fetch($template, $vars, $replace, $config);
                } else {
                    exit($template . '不存在！');
                }
            }
        } else {
            return parent::fetch($template, $vars, $replace, $config);
        }


    }

    /**
     * 是否司机
     */
    public function isDriver()
    {
        $carDriverModel = new CarDriverModel();
        $aid = $carDriverModel->where('bind_uid', $this->getUid())->value('aid');
        return $aid>0 ? $aid : 0;
    }

    /**
     * 按权限读取列表
     */
    public function getOp()
    {
        $userModel = new UserModel();
        $map = [];
        if ($this->request->has('op')) {
            $op = $this->request->param('op');
            $map = $userModel->getOp($op, $this->getUid(), $this->getDeptIds());
        }
        return $map;
    }

    /**
     * 获取用户类型
     * @return string
     */
    public function getUtype()
    {
        return 'system';
    }

    /**
     * 获取用户UID
     * @return bool|mixed
     */
    public function getUid()
    {
        return $this->uid;
    }

    /**
     * 获取当前登录部门ID
     * @return mixed
     */
    public function getDeptIds()
    {
        return $this->deptIds;
    }

    /**
     * 获取权限组ID
     */
    public function getGroupId()
    {
        if (!Session::has('group_id')) {
            $userModel = new UserModel();
            $groupId = $userModel->where('uid', $this->getUid())->value('group_id');
            Session::set('group_id', $groupId);
            return $groupId;
        }
        return Session::get('group_id');
    }

}